package com.cu.employmentmanagementsystem.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.cu.employmentmanagementsystem.common.convert.UserDTOConverter;
import com.cu.employmentmanagementsystem.common.exception.CommonException;
import com.cu.employmentmanagementsystem.common.result.R;
import com.cu.employmentmanagementsystem.dto.UserDTO;
import com.cu.employmentmanagementsystem.entity.User;
import com.cu.employmentmanagementsystem.service.UserService;
import com.google.common.base.Preconditions;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 25853
 */
@Api(value = "用户管理接口", tags = "UserController")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController
{
    @Resource
    private UserService userService;

    @ApiOperation(value = "用户注册", notes = "接收UserDTO对象进行用户注册")
    @ApiResponses({
            @ApiResponse(code = 200, message = "注册成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody UserDTO userDTO)
    {
        try
        {
            Preconditions.checkArgument(!StringUtils.isBlank(userDTO.getAccount()), "账号不可为空");
            Preconditions.checkArgument(!StringUtils.isBlank(userDTO.getPassword()), "密码不可为空");
            Preconditions.checkNotNull(userDTO.getRoleIndex(), "角色索引不可为空");

            Boolean result = userService.register(userDTO);
            return R.success(result);
        }
        catch (Exception e)
        {
            throw e;
        }
    }


    @ApiOperation(value = "用户修改", notes = "接收UserDTO对象进行用户修改")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @PostMapping("/update")
    @SaCheckLogin
    public R<Boolean> update(@RequestBody UserDTO userDTO)
    {
        try
        {
            Preconditions.checkNotNull(userDTO.getAccount(), "账号不可为空");
            Preconditions.checkNotNull(userDTO.getRole(), "角色不可为空");
            Boolean result = userService.update(userDTO);
            return R.success(result);
        }
        catch (Exception e)
        {
            throw new CommonException("修改失败");
        }
    }

    @ApiOperation(value = "用户删除", notes = "接收UserDTO对象进行用户删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @PostMapping("/delete")
    @SaCheckLogin
    public R<Boolean> delete(@RequestBody UserDTO userDTO)
    {
        try
        {
            Preconditions.checkNotNull(userDTO.getId(), "id不可为空");
            User user = UserDTOConverter.INSTANCE.convertDTOToEntity(userDTO);
            Boolean result = userService.delete(user);
            return R.success(result);
        }
        catch (Exception e)
        {
            throw new CommonException("删除失败");
        }
    }

    @ApiOperation(value = "用户启用与禁用", notes = "接收UserDTO对象进行用户启用与禁用")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @SaCheckLogin
    @PostMapping("/changeStatus")
    public R<Boolean> changeStatus(@RequestBody UserDTO userDTO)
    {
        try
        {
            Preconditions.checkNotNull(userDTO.getAccount(), "id不可为空");
            Preconditions.checkNotNull(userDTO.getStatus(), "状态不可为空");
            Boolean result = userService.update(userDTO);
            return R.success(result);
        }
        catch (Exception e)
        {
            throw new CommonException("修改状态失败");
        }
    }

    @ApiOperation(value = "扫码登录", notes = "接收validCode进行扫码登录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "登录成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @PostMapping("/loginByValidCode")
    public R<SaTokenInfo> loginByValidCode(@RequestParam String validCode)
    {
        try
        {
            SaTokenInfo tokenInfo = userService.loginByValidCode(validCode);
            return R.success(tokenInfo);
        }
        catch (Exception e)
        {
            throw new CommonException("登陆失败");
        }
    }

    @ApiOperation(value = "密码登录", notes = "接收UserDTO对象进行密码登录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "登录成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @PostMapping("/loginByPassword")
    public R<SaTokenInfo> loginByPassword(@RequestBody UserDTO userDTO)
    {
        try
        {
            Preconditions.checkNotNull(userDTO.getAccount(), "账号不可为空");
            Preconditions.checkNotNull(userDTO.getPassword(), "密码不可为空");
            User user = UserDTOConverter.INSTANCE.convertDTOToEntity(userDTO);
            SaTokenInfo tokenInfo = userService.loginByPassword(user);
            return R.success(tokenInfo);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    @ApiOperation(value = "获取用户信息", notes = "接收UserDTO对象进行获取用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @GetMapping("/info")
    @SaCheckLogin
    public R<UserDTO> info()
    {
        try
        {
            UserDTO resultUserDTO = userService.getUserInfo();
            return R.success(resultUserDTO);
        }
        catch (Exception e)
        {
            throw new CommonException("获取用户信息失败");
        }
    }

    @ApiOperation(value = "注销", notes = "注销")
    @ApiResponses({
            @ApiResponse(code = 200, message = "注销成功"),
            @ApiResponse(code = 500, message = "请求参数不合法")
    })
    @PostMapping("/logout")
    @SaCheckLogin
    public R<Boolean> logout()
    {
        try
        {
            StpUtil.logout();
            return R.success(true);
        }
        catch (Exception e)
        {
            throw new CommonException("注销失败");
        }
    }

    @ApiOperation(value = "获取学生就业流程步骤", notes = "获取学生就业流程步骤")
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 500, message = "服务器异常")
    })
    @GetMapping("getFlowIndex")
    @SaCheckLogin
    public R<Integer> getFlowIndex()
    {
        try
        {
            Integer result = userService.getFlowIndex();
            return R.success(result);
        }
        catch (Exception e)
        {
            throw new CommonException("获取学生就业流程步骤失败");
        }
    }
}
